﻿using DevExpress.DirectX.Common;
using DevExpress.Office.Utils;
using Marmat.Forms;
using Marmat.Forms.Skin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 测试项目
{
    public partial class ControlTestForm : System.Windows.Forms.Form
    {
        public ControlTestForm()
        {
            InitializeComponent();
            //this.textBox1.PasswordChar
        }

        private void ControlTestForm_Load(object sender, EventArgs e)
        {

        }

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
            base.OnPaint(e);
            Rectangle rect = new Rectangle(700, 200, 50, 50);
            GraphicsPath path = new GraphicsPath();
            var path1 = rect.GetRoundPath();
            var rect2 = new Rectangle(690, 200, 50, 50);
            var path2 = rect2.GetRoundPath();
            Region region1 = new Region(path1);
            Region region2 = new Region(path2);

            region2.Intersect(region1);


            //e.Graphics.FillPath(Brushes.Green,path3);
            e.Graphics.FillRegion(Brushes.Green, region2);

        }


        public void Test()
        {
            #region  把region类型转为pointF类型
            //GraphicsPath Gpath1 = new GraphicsPath();
            //Matrix matrix1 = new Matrix();
            //RectangleF[] Rpath = region2.GetRegionScans(matrix1);
            //Gpath1.AddRectangles(Rpath);
            //PointF[] pts_inter = Gpath1.PathPoints;
            #endregion
            //int n_inter = pts_inter.Length;
            //PointF[] pts_sort = new PointF[n_inter / 2 + 2];
            //pts_sort[n_inter / 4] = pts_inter[n_inter - 1];   //(n-1)
            //pts_sort[n_inter / 4 + 1] = pts_inter[n_inter - 2];  //(n-2)
            //for (int i = 0; i < n_inter; i++)
            //{
            //    if (i % 4 == 1)
            //    {
            //        if (i == 1)
            //            pts_sort[(i - 1) / 4] = pts_inter[i];
            //        else
            //            pts_sort[(i - 1) / 4] = pts_inter[i].X > pts_inter[i - 3].X ? pts_inter[i] : pts_inter[i - 3];
            //    }
            //    else if (i % 4 == 0)
            //    {
            //        if (i == 0)
            //            pts_sort[n_inter / 2 + 1 - i / 4] = pts_inter[i];
            //        else
            //            pts_sort[n_inter / 2 + 1 - i / 4] = pts_inter[i].X < pts_inter[i - 1].X ? pts_inter[i] : pts_inter[i - 1];
            //    }

            //}
            #region 插值
            //int n_sort = pts_sort.Length;
            //PointF[] ptsLinear = new PointF[n_sort * 2];
            //for (int j = 0; j < n_sort * 2; j++)
            //{
            //    if (j % 2 == 0)
            //    {
            //        ptsLinear[j] = pts_sort[j / 2];
            //    }
            //    else
            //    {
            //        if (j == n_sort * 2 - 1)
            //        {
            //            ptsLinear[j].X = (float)0.5 * (pts_sort[0].X + pts_sort[n_sort - 1].X);
            //            ptsLinear[j].Y = (float)0.5 * (pts_sort[0].Y + pts_sort[n_sort - 1].Y);
            //        }
            //        else
            //        {
            //            int i_front = (j - 1) / 2;
            //            int i_back = (j + 1) / 2;
            //            ptsLinear[j].X = (float)0.5 * (pts_sort[i_front].X + pts_sort[i_back].X);
            //            ptsLinear[j].Y = (float)0.5 * (pts_sort[i_front].Y + pts_sort[i_back].Y);
            //        }
            //    }

            //}
            #endregion
        }
        /// <summary>
        /// Calculation of two colors blend of new color values
        /// R,G,B∈(0 , 255)     A∈(0 , 1) 
        ///  Color(A,R,G,B) = Color(A1,R1,G1,B1) + Color(A2,R2,G2,B2)
        /// </summary>
        /// <param name="cor1"></param>
        /// <param name="cor2"></param>
        /// <returns>blend color</returns>
        public Color ColorBlend(Color color1, Color color2)
        {

            float a1 = color1.A;
            float a2 = color2.A - (a1 * color2.A) / 256;
            float a = a1 + a2;

            int R = Convert.ToInt32((a1 * color1.R + a2 * color2.R) / a);
            var G = Convert.ToInt32((a1 * color1.G + a2 * color2.G) / a);
            var B = Convert.ToInt32((a1 * color1.B + a2 * color2.B) / a);
            var A = Convert.ToInt32(a);
            return Color.FromArgb(A, R, G, B);
        }
        /// <summary>
        /// Through two kinds of color's R/G/B and Alpha, calculate a new value
        /// R/G/B use the same formula
        /// For example:
        ///     R : blend Red ; 
        ///     A : blend alpha ;
        ///     R = (a1 * R1 + (1 - a1) * a2 * R2) / A
        /// </summary>
        /// <param name="a1">Alpha1</param>
        /// <param name="a2">Alpha2</param>
        /// <param name="c1">color's R/G/B</param>
        /// <param name="c2">color's R/G/B</param>
        /// <returns>nwe color's R/G/B</returns>
        public double ColorRGBBlendCalculate(double a1, double a2, double c1, double c2)
        {
            return (a1 * c1 + (1d - a1) * a2 * c2) / ColorAlphaBlendCalculate(a1, a2);
        }

        public double ColorAlphaBlendCalculate(double alpha1, double alpha2)
        {
            return alpha1 + alpha2 - alpha1 * alpha2;
        }


        private void ControlTestForm_Paint(object sender, PaintEventArgs e)
        {

        }

        private async void skButton1_Click(object sender, EventArgs e)
        {
            this.skButton1.Loading = true;
            this.skButton1.Invalidate();
            //Task task = new Task(callCount);
            await callCount();
            this.skButton1.Invalidate();
            this.skButton1.Loading = false;
        }

        private void skButton2_Click(object sender, EventArgs e)
        {
            NotificationForm notification = new NotificationForm();
            notification.Show();
        }
        private async Task callCount()
        {
            //Task task = new Task(count);
            //task.Start();
            await Task.Run(() => System.Threading.Thread.Sleep(5000));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.skButton1.Enabled = !this.skButton1.Enabled;
            this.skButton3.Enabled = !this.skButton3.Enabled;
        }




        public void SetWindowRegion()
        {
            System.Drawing.Drawing2D.GraphicsPath FormPath;
            FormPath = new System.Drawing.Drawing2D.GraphicsPath();
            Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
            FormPath = GetRoundedRectPath(rect, 60);
            this.Region = new Region(FormPath);



        }
        private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            int diameter = radius;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();

            // 左上角
            path.AddArc(arcRect, 180, 90);

            // 右上角
            arcRect.X = rect.Right - diameter;
            path.AddArc(arcRect, 270, 90);

            // 右下角
            arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 0, 90);

            // 左下角
            arcRect.X = rect.Left;
            path.AddArc(arcRect, 90, 90);
            path.CloseFigure();//闭合曲线
            return path;
        }


        private void skButton3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void switch1_Click(object sender, EventArgs e)
        {

        }



        private void skinButton1_Click(object sender, EventArgs e)
        {
            this.progress1.Percentage += 1;
        }

        private void skinButton2_Click(object sender, EventArgs e)
        {
            this.progress1.Percentage -= 1;
        }

        private void progress1_PercentageChange(object sender, EventArgs e)
        {

            if (this.progress1.Percentage > 80)
            {
                this.progress1.ThemeStyle = ThemeStyleEnum.Success;
            }
            else if (this.progress1.Percentage > 50)
            {
                this.progress1.ThemeStyle = ThemeStyleEnum.Info;
            }
            else if (this.progress1.Percentage > 20)
            {
                this.progress1.ThemeStyle = ThemeStyleEnum.Primary;
            }
            else if (this.progress1.Percentage <= 15)
            {
                this.progress1.ThemeStyle = ThemeStyleEnum.Primary;
            }
        }

        private void buttonGroup3_ItemClicked(object sender, ButtonGroupEventArgs e)
        {
            //this.button2.TextImageRelation
            //this.linkLabel1.LinkColor
        }
    }
}
